<template>
  <div class="custom-table-list-wrapper">
    <div class="search search1" :class="{ active: toggleSearchStatus }">
      <div class="search-left" ref="searchFromRef">
        <el-form class="search-form" inline>
          <el-form-item label="产权单位">
            <el-select
              class="search-select"
              v-model="searchParams.propertyOwner"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of dictOptions['propertyUnit']"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否危房">
            <el-select
              class="search-select"
              v-model="searchParams.dangerHouse"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of dictOptions['yn']"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否重点区域">
            <el-select
              class="search-select"
              v-model="searchParams.keyArea"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of dictOptions['yn']"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              class="search-input"
              v-model="searchParams.name"
              placeholder="请输入房屋名称、不动产权证号"
              :suffix-icon="Search"
            ></el-input>
          </el-form-item>
        </el-form>
        <a
          class="search-left-toggle"
          v-if="showToggle"
          @click="handleToggleSearch"
        >
          <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
          {{ toggleSearchStatus ? '收起' : '展开' }}
        </a>
      </div>
      <div class="search-right">
        <el-button type="primary" @click="initTableList(1)"
          ><svg-icon name="search" size="14px" />搜索</el-button
        >
        <el-button @click="handleReset()"
          ><svg-icon name="reset" size="17px" />重置</el-button
        >
      </div>
    </div>
    <div class="content">
      <div class="content-operate">
        <el-button
          type="primary"
          @click="handleAdd('buildingAdd', { parkId: parkId }, true)"
          ><svg-icon name="add" size="15px" />新增房屋</el-button
        >
      </div>
      <div class="content-table">
        <el-table class="custom-table" :data="tableData">
          <el-table-column type="index" label="序号" width="60px" />
          <el-table-column prop="name" label="房屋名称" />
          <el-table-column prop="address" label="房屋地址" />
          <el-table-column prop="num" label="入驻企业数" />
          <el-table-column prop="dangerHouse" label="是否危房">
            <template #default="scope">
              {{ getDictValue('yn', scope.row.dangerHouse) }}
              <el-tag
                v-if="scope.row.dangerLevel"
                :type="getDictStyle('buildingLevel', scope.row.dangerLevel)"
                >{{
                  getDictValue('buildingLevel', scope.row.dangerLevel)
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="keyArea" label="是否重点区域">
            <template #default="scope">
              {{ getDictValue('yn', scope.row.keyArea) }}
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="更新时间" />
          <el-table-column label="操作" width="160px">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="
                  handleDetail({ pkid: scope.row.pkid }, 'buildingDetail', true)
                "
                >详情</el-button
              >
              <el-button
                link
                type="primary"
                @click="
                  handleEdit({ pkid: scope.row.pkid }, 'buildingAdd', true)
                "
                >编辑</el-button
              >
              <el-button link type="danger" @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="content-page">
        <el-pagination
          class="custom-pagination"
          :current-page="paginationParams.pageIndex"
          :page-size="paginationParams.pageSize"
          background
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="initTableList"
        />
      </div>
    </div>
  </div>
</template>
<script setup>
import { onMounted, ref, watch } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useDict } from '@/hooks/useDict'
import {
  buildingPageList,
  buildingDelete,
} from '@/api/manager/dataManage/building'
const {
  dictOptions,
  initListDictOptions,
  initTreeDictOptions,
  getDictValue,
  getDictStyle,
} = useDict()
const props = defineProps({
  parkId: {
    type: String,
  },
})
const searchParams = ref({
  parkId: '',
})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, buildingPageList, buildingDelete)
watch(
  () => props.parkId,
  (newVal) => {
    searchParams.value.parkId = newVal
    initTableList(1)
  },
  {
    immediate: true,
  },
)
defineExpose({ initTableList })
onMounted(() => {
  initListDictOptions(['propertyUnit', 'yn'])
})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';

.content-table {
  height: calc(100% - 50px) !important;
}

.warning1 {
  color: #eb373a;
  display: flex;
  align-items: center;

  .svg-icon {
    margin-left: 5px;
    margin-top: 3px;
  }
}

.warning2 {
  color: #ffd144;
  display: flex;
  align-items: center;

  .svg-icon {
    margin-left: 5px;
    margin-top: 3px;
  }
}

.warning3 {
  color: #ca8d2d;
  display: flex;
  align-items: center;

  .svg-icon {
    margin-left: 5px;
    margin-top: 3px;
  }
}

.custom-table {
  position: absolute;
}
</style>
